package com.liux.rabbitmq.three;

import com.liux.rabbitmq.utils.RabbitMQUtil;
import com.liux.rabbitmq.utils.SleepUtil;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @Date 2022/4/16 23:02
 * @Description
 * 消费者，手动应答，消息不丢失，放回队列
 **/
public class Work03 {
    public static final String TASK_QUEUE_NAME ="ACK_QUEUE";

    public static void main(String[] args) throws Exception{
        Channel channel = RabbitMQUtil.getChannel();
        System.out.println("C3开始等待接收消息,沉睡时间短");
        DeliverCallback deliverCallback =(var1 ,var2) ->{
            //沉睡1S
            SleepUtil.sleep(1);
            System.out.println("消息回调接口，接收到的消息为："+new String(var2.getBody()));
            //手动应答
            /**
             * 1、消息的标机 tag 上图的 5 6 7 8
             * 2、是否批量应答 false：不批量
             */
            channel.basicAck(var2.getEnvelope().getDeliveryTag(),false);
        };

        CancelCallback cancelCallback =(var1 ) ->{
            System.out.println("取消消费回调接口");
        };
        //不公平分发，抢占式
        //1:表示不公平分发，默认是0 轮询分发
        //int prefetchCount =1;
        //其他数字表示预期值，即消息挤压数量.此处表示可以积压2条
        int prefetchCount =2;
        channel.basicQos(prefetchCount);
        //不自动应答，false
        channel.basicConsume(TASK_QUEUE_NAME,false,deliverCallback, cancelCallback);

    }
}
